package com.cms.manage.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 
 * 
 * @author sgy
 * 
 */
public class DateTimeUtil {

	
	public static void main(String[] args){
		
		
		System.out.println(DateTimeUtil.getLastDateByDate(DateTimeUtil.getStrByDateType(new Date(),"yyyy-MM-dd"),0,"yyyy-MM-dd", "yyyy_MM"));
		/*Date d1 = new Date();
		System.out.println(DateTimeUtil.dateConvtoFmt(d1,"yyyy-MM-dd HH:mm:ss"));
		Date d2 = DateTimeUtil.setLastTime(d1);
		System.out.println(DateTimeUtil.dateConvtoFmt(d2,"yyyy-MM-dd HH:mm:ss"));*/
		/*String type ="yyyy-MM-dd";		
		Date date=DateTimeUtil.getDateByStrType("2018-01-10", type);
		 Calendar now = Calendar.getInstance();
	     now.setTime(date);
	     now.add(now.MONTH, -1);
	     SimpleDateFormat dft = new SimpleDateFormat("yyyy_MM");
	     String lastMonth = dft.format(now.getTime());
         System.out.println(lastMonth);*/
	     
	}
	
	//获取当天时间
	public static String getToday() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dt = sdf.format(date);
		return dt;
	}
	//根据时间获取年月日 时分秒
	  public static String getDateYMDHMS(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmss");
		return sdf.format(date);
	 }
	//根据时间获取年月日
	public static String getDateYMD(Date date) {
		    Calendar now = Calendar.getInstance();
	        now.setTime(date);
	        int year = now.get(Calendar.YEAR);
	        int month = now.get(Calendar.MONTH) + 1; // 0-based!
	        int day = now.get(Calendar.DAY_OF_MONTH);
			return year+"-"+month+"-"+day;
   }
	//根据时间获取年月
		public static String getDateYM(Date date) {
			    Calendar now = Calendar.getInstance();
		        now.setTime(date);
		        int year = now.get(Calendar.YEAR);
		        int month = now.get(Calendar.MONTH) + 1; // 0-based!
		        int day = now.get(Calendar.DAY_OF_MONTH);
				return year+"-"+month;
	   }
		//获取当天时间
		public static String getStrByDateType(Date date,String type) {
			SimpleDateFormat sdf = new SimpleDateFormat(type);   
			String dt = sdf.format(date);
			return dt;
		}	
	//根据字符串时间获取日期
	public static Date getDateByStrType(String date,String type) {
		SimpleDateFormat sdf = new SimpleDateFormat(type);   
		 Date dateParse = new Date();
		try {
			dateParse = sdf.parse(date);
		} catch (ParseException e) {	
			e.printStackTrace();
			return null;
		}  		  
	     return dateParse;     
	   }
	   //根据字符串时间获取上count个月
		public static String getLastDateByDate(String currentDate,int count,String currentType,String lastType) {		
			 Date date=DateTimeUtil.getDateByStrType(currentDate, currentType);
			 Calendar now = Calendar.getInstance();
		     now.setTime(date);
		     now.add(now.MONTH, -count);
		     SimpleDateFormat dft = new SimpleDateFormat(lastType);
		     String lastMonth = dft.format(now.getTime());
	         return lastMonth;
	  }
		
	
}